<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>管理员权限管理</title>
  <link rel="stylesheet" href="../css/backstage_admin_quanxian.css">
  <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script src="../js/jquery-3.7.1.min.js"></script>
  <style>
    .table-container {
      width: 1200px;
      height: 700px;
      margin: 100px;
      padding: 20px;
      background-color: #f9f9f9;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    table {
      width: 800px;
      border-collapse: collapse;
      margin: 60px 0;
      margin-left: 100px;
    }
    table th, table td {
      padding: 10px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }
    table th {
      background-color: #35495d;
      color: white;
    }
    table tr:hover {
      background-color: #f1f1f1;
    }
    .super-admin-checkbox {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>
<body>
<div class="sidebar">
  <input type="checkbox" id="checkbox">
  <label for="checkbox">
    <i class="fa fa-bars" aria-hidden="true"></i>
  </label>
  <ul>
    <li>
      <img src="../images/people.png">
      <span>欢迎你,管理员!</span>
    </li>
    <li>
      <a href="admin_index.html">
        <i class="fa fa-home" aria-hidden="true"></i>
        <span>后台首页</span>
      </a>
    </li>
    <li>
      <a href="backstage_admin.html">
        <i class="fa fa-user-circle-o" aria-hidden="true"></i>
        <span>管理员列表</span>
      </a>
    </li>
    <li>
      <a href="backstage_admin_quanxian.html">
        <i class="fa fa-user-circle-o" aria-hidden="true"></i>
        <span>管理员权限分配</span>
      </a>
    </li>
    <li>
      <a href="user.html">
        <i class="fa fa-user-circle-o" aria-hidden="true"></i>
        <span>用户列表</span>
      </a>
    </li>
    <li>
      <a href="products_admin.html">
        <i class="fa fa-sitemap" aria-hidden="true"></i>
        <span>道具列表</span>
      </a>
    </li>
    <li>
      <a href="people.html">
        <i class="fa fa-shopping-bag" aria-hidden="true"></i>
        <span>开发人员</span>
      </a>
    </li>
    <li>
      <a href="index.html">
        <i class="fa fa-th-large" aria-hidden="true"></i>
        <span>我要退出！</span>
      </a>
    </li>
  </ul>
</div>

<div class="table-container">
  <h2>管理员权限管理</h2>
  <table id="adminTable">
    <thead>
    <tr>
      <th>管理员序号</th>
      <th>管理员名称</th>
      <th>超级管理员</th>
    </tr>
    </thead>
    <tbody>
    <!-- 这里使用 JavaScript 动态添加管理员信息 -->
    </tbody>
  </table>
</div>

<script>
  $(document).ready(function () {
    let currentUserGrade = null;

    // 获取当前登录管理员的grade
    $.get("/admin/grade", function (data) {
      currentUserGrade = data;
    });

    $.get("/admin/all", function (data) {
      // 清空表格内容
      $("#adminTable tbody").empty();

      // 遍历管理员信息并添加到表格中
      data.forEach(function (admin) {
        var row = "<tr>" +
                "<td>" + admin.adminId + "</td>" +
                "<td>" + admin.adminName + "</td>" +
                "<td class='super-admin-checkbox'><input type='checkbox' data-id='"
                + admin.adminId + "' " + (admin.grade === 1 ? "checked" : "") + "></td>"
                +"</tr>";
        $("#adminTable tbody").append(row);
      });

      // 监听复选框变化
      $("#adminTable").on('change', 'input[type="checkbox"]', function () {
        if (currentUserGrade !== 1) {
          alert('您不是超级管理员，你没有权限更改！');
          $(this).prop('checked', !$(this).is(':checked')); // 恢复复选框的原始状态
          return;
        }

        var adminId = $(this).data('id');
        var isChecked = $(this).is(':checked') ? 1 : 0;

        var confirmationMessage = isChecked ? "是否确定？" : "是否确定？";

        if (confirm(confirmationMessage)) {
          $.ajax({
            url: '/admin/updateGrade',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ adminId: adminId, grade: isChecked }),
            success: function (response) {
              console.log('Grade updated successfully');
            },
            error: function (error) {
              console.log('Error updating grade');
            }
          });
        } else {
          // 如果用户取消确认，则恢复复选框的原始状态
          $(this).prop('checked', !isChecked);
        }
      });
    });
  });
</script>
</body>
</html>
